//
//  PywValidSmsCodeViewController.m
//  PywSdk
//
//  Created by smallwave on 16/8/26.
//  Copyright © 2016年 zero. All rights reserved.
//

#import "PywValidSmsCodeViewController.h"
#import "PywMasonry.h"
#import "UILabel+Pyw.h"
#import "PywTextFieldView.h"
#import "UIButton+Pyw.h"
#import "PywCommonManager.h"
#import "PywGetSmsCodeApi.h"
#import "PywMBProgressHUD+Pyw.h"
#import "PywResourceUtils.h"
#import "PywCheckSmsCodeApi.h"
#import "PywValidProblemViewController.h"
#import "MutableDictionary.h"

@interface PywValidSmsCodeViewController ()<PywTextFieldViewDelegate>

@property (nonatomic, strong) UILabel               *propmtLabel;       // 提示信息
@property (nonatomic, strong) PywTextFieldView      *validCodeView;     // 验证码输入框
@property (nonatomic, strong) UIButton              *nextBtn;           // 下一步按钮

@property (nonatomic, copy) NSString                *phoneNumber;       // 手机号码
@property (nonatomic, copy) NSString                *smsType;           // 获取验证码场景类型

@property (nonatomic, strong) PywGetSmsCodeApi      *getSmsCodeApi;     // 获取验证码请求
@property (nonatomic, strong) PywCheckSmsCodeApi    *checkSmsCodeApi;   // 校验验证码请求

@end

@implementation PywValidSmsCodeViewController
KeyBoardHelper(90)

#pragma mark - Init Method
- (instancetype)initWithPhoneNumber:(NSString *)phoneNumber smsType:(NSString *)smsType{
    self = [super init];
    if (self) {
        self.phoneNumber = phoneNumber;
        self.smsType = smsType;
    }
    return self;
}

#pragma mark - Lift Cycle Method
- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setUp];
}

#pragma mark - prepareUI
- (void)setUp{
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"填写验证码";
    self.navigationItem.rightBarButtonItem = nil;
    self.automaticallyAdjustsScrollViewInsets = NO;
    [[PywCommonManager manager] startTimeWithButton:self.validCodeView.operationBtn];
    
    [self addSubviewsAndLayoutSubviews];
}
- (void)addSubviewsAndLayoutSubviews{
    
    [self.view addSubview:self.propmtLabel];
    [self.view addSubview:self.validCodeView];
    [self.view addSubview:self.nextBtn];
    
    __weak typeof(self) wSelf = self;
    [self.propmtLabel mas_makeConstraints:^(PywMASConstraintMaker *make) {
        make.centerX.offset(0);
        make.top.equalTo(wSelf.view.mas_top).offset(80);
        make.width.equalTo(wSelf.view.mas_width).multipliedBy(0.85);
    }];
    
    [self.validCodeView mas_makeConstraints:^(PywMASConstraintMaker *make) {
        make.centerX.offset(0);
        make.top.equalTo(wSelf.propmtLabel.mas_bottom).offset(20);
        make.width.equalTo(wSelf.propmtLabel.mas_width);
        make.height.equalTo(@40);
    }];
    
    [self.nextBtn mas_makeConstraints:^(PywMASConstraintMaker *make) {
        make.centerX.offset(0);
        make.top.equalTo(wSelf.validCodeView.mas_bottom).offset(40);
        make.width.height.equalTo(wSelf.validCodeView);
    }];
}

#pragma mark - Actoin
- (void)nextBtnActoin{
    [self.view endEditing:YES];
    // 判断是否已填写验证码
    if (self.validCodeView.text.length <= 0) {
        
        [PywMBProgressHUD showError:@"请填写验证码" toView:self.view];
    } else {
        
        [self.checkSmsCodeApi requestCheckSmsCodeWithParams:[self requestCheckValidCodeParams] success:^(NSString *status, id anyObjct) {
            
            PywValidProblemViewController *setTradePwdVC = [[PywValidProblemViewController alloc] init];
            [self.navigationController pushViewController:setTradePwdVC animated:YES];
            
        } fail:^(NSError *error) {
            self.validCodeView.text = @"";
        }];
    }
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.validCodeView becomeFirstResponder];
}
/**
 *  校验短信验证码  传入参数
 *
 *  @return <#return value description#>
 */
- (MutableDictionary *)requestCheckValidCodeParams{
    NSString *smsCode = [self.validCodeView.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    NSString *phoneNumber = [self.phoneNumber stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    NSString *smsType = [self.smsType stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    
    MutableDictionary *param = [[MutableDictionary alloc] init];
    [param setObject:phoneNumber forKey:@"phone"];
    [param setObject:smsCode forKey:@"smscode"];
    [param setObject:smsType forKey:@"smstype"];
    return param;
}

#pragma mark - PywTextFieldViewDelegate
- (void)textFieldView:(PywTextFieldView *)textField buttonEvent:(UIButton *)btn{
    
    [self.getSmsCodeApi requestGetSmsCodeWithParams:[self requestGetValidCodeParams] success:^(NSString *status, id anyObjct) {
        
        [[PywCommonManager manager] startTimeWithButton:self.validCodeView.operationBtn];
    } fail:^(NSError *error) {
        
    }];
}
/**
 *  获取短信验证码  传入参数
 *
 *  @return <#return value description#>
 */
- (MutableDictionary *)requestGetValidCodeParams{
    NSString *phoneNumber = [self.phoneNumber stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    NSString *smsType = [self.smsType stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    
    MutableDictionary *param = [[MutableDictionary alloc] init];
    [param setObject:phoneNumber forKey:@"phone"];
    [param setObject:smsType forKey:@"smstype"];
    return param;
}

#pragma mark - Setter & Getter
- (UILabel *)propmtLabel{
    if (!_propmtLabel) {
        _propmtLabel = [UILabel labelWithText:nil
                                textAlignment:NSTextAlignmentLeft
                                     fontSize:12
                                    textColor:[UIColor blackColor]];
        
        NSString *phone = self.phoneNumber;
        NSString *propmtStr = [NSString stringWithFormat:@"已向%@发送验证码",phone];
        NSMutableAttributedString *propmtMessage = [[NSMutableAttributedString alloc] initWithString:propmtStr];
        [propmtMessage addAttribute:NSForegroundColorAttributeName value:RGB(252, 177, 14, 1) range:NSMakeRange(2, phone.length)];
        _propmtLabel.attributedText = propmtMessage;
    }
    return _propmtLabel;
}
- (PywTextFieldView *)validCodeView{
    if (!_validCodeView) {
        _validCodeView = [PywTextFieldView validCodeTextFieldWithLeftImg:nil
                                                         TextPlaceholder:@"请输入验证码"
                                                                Delegate:self
                                                             ButtonTitle:@"获取验证码"];
    }
    return _validCodeView;
}
- (UIButton *)nextBtn{
    if (!_nextBtn) {
        _nextBtn = [UIButton buttonWithBackgroundColor:RGB(252, 177, 14, 1)
                                          cornerRadius:20
                                            titleColor:[UIColor whiteColor]
                                    titleLabelFontSize:18
                                                 title:@"下一步"];
        
        [_nextBtn addTarget:self action:@selector(nextBtnActoin) forControlEvents:UIControlEventTouchUpInside];
    }
    return _nextBtn;
}
- (PywGetSmsCodeApi *)getSmsCodeApi{
    if (!_getSmsCodeApi) {
        _getSmsCodeApi = [PywGetSmsCodeApi manager];
    }
    return _getSmsCodeApi;
}
- (PywCheckSmsCodeApi *)checkSmsCodeApi{
    if (!_checkSmsCodeApi) {
        _checkSmsCodeApi = [PywCheckSmsCodeApi manager];
    }
    return _checkSmsCodeApi;
}

@end
